package ohc.login

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import net.fabricmc.loader.api.FabricLoader
import java.io.File

/**
 * 配置文件管理类
 */
object Config {
    private val gson: Gson = GsonBuilder().setPrettyPrinting().create()
    private val configFile: File = FabricLoader.getInstance().configDir.resolve("ohc_login.json").toFile()
    
    var apiBaseUrl: String = "https://api.ottohub.cn"  // API 基础地址
    var loginTimeout: Int = 300  // 登录超时时间（秒）
    var kickOnTimeout: Boolean = true  // 超时是否踢出玩家
    
    var wrongPasswordCooldownAttempts: Int = 2  // 每输错X次密码触发冷却
    var wrongPasswordCooldownSeconds: Int = 60  // 冷却时间（秒）
    var wrongPasswordKickAttempts: Int = 5  // 共输错X次踢出服务器
    var wrongPasswordKickHours: Int = 24  // 踢出后禁止登录时间（小时）
    var maxBindPerUid: Int = 3  // 每个UID最多绑定玩家数
    var enableBackupLogin: Boolean = true  // 是否启用备用登录功能
    
    // 自动存档配置
    var autoBackupEnabled: Boolean = true  // 是否启用自动存档
    var autoBackupHour: Int = 0  // 自动存档小时 (0-23)
    var autoBackupMinute: Int = 0  // 自动存档分钟 (0-59)
    
    /**
     * 加载配置文件
     */
    fun load() {
        if (configFile.exists()) {
            try {
                val data = gson.fromJson(configFile.readText(), ConfigData::class.java)
                apiBaseUrl = data.apiBaseUrl
                loginTimeout = data.loginTimeout
                kickOnTimeout = data.kickOnTimeout
                wrongPasswordCooldownAttempts = data.wrongPasswordCooldownAttempts
                wrongPasswordCooldownSeconds = data.wrongPasswordCooldownSeconds
                wrongPasswordKickAttempts = data.wrongPasswordKickAttempts
                wrongPasswordKickHours = data.wrongPasswordKickHours
                maxBindPerUid = data.maxBindPerUid
                enableBackupLogin = data.enableBackupLogin
                autoBackupEnabled = data.autoBackupEnabled
                autoBackupHour = data.autoBackupHour
                autoBackupMinute = data.autoBackupMinute
                // 重新保存以补充可能缺失的新字段
                save()
            } catch (e: Exception) {
                Ohc_login.logger.error("加载配置文件失败: ${e.message}")
                save()
            }
        } else {
            save()
        }
    }
    
    /**
     * 保存配置文件
     */
    fun save() {
        try {
            configFile.parentFile?.mkdirs()
            val data = ConfigData(
                apiBaseUrl, loginTimeout, kickOnTimeout,
                wrongPasswordCooldownAttempts, wrongPasswordCooldownSeconds, wrongPasswordKickAttempts, wrongPasswordKickHours,
                maxBindPerUid, enableBackupLogin,
                autoBackupEnabled, autoBackupHour, autoBackupMinute
            )
            configFile.writeText(gson.toJson(data))
        } catch (e: Exception) {
            Ohc_login.logger.error("保存配置文件失败: ${e.message}")
        }
    }
    
    /**
     * 配置数据类
     */
    private data class ConfigData(
        val apiBaseUrl: String = "https://api.ottohub.cn",
        val loginTimeout: Int = 300,
        val kickOnTimeout: Boolean = true,
        val wrongPasswordCooldownAttempts: Int = 2,
        val wrongPasswordCooldownSeconds: Int = 60,
        val wrongPasswordKickAttempts: Int = 5,
        val wrongPasswordKickHours: Int = 24,
        val maxBindPerUid: Int = 3,
        val enableBackupLogin: Boolean = true,  // 是否启用备用登录功能
        val autoBackupEnabled: Boolean = true,  // 是否启用自动存档
        val autoBackupHour: Int = 0,  // 自动存档小时
        val autoBackupMinute: Int = 0  // 自动存档分钟
    )
}
